package com.globalresources.ui.struts.action.message;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.acegisecurity.context.SecurityContextHolder;
import org.acegisecurity.userdetails.UserDetails;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.globalresources.business.inface.IMessageService;
import com.globalresources.business.inface.IUserService;
import com.globalresources.commons.AbstractAction;
import com.globalresources.commons.Constant;
import com.globalresources.commons.Pager;
import com.globalresources.commons.PagerService;
import com.globalresources.commons.StringHelper;
import com.globalresources.po.Message;
import com.globalresources.po.MessageReceiver;
import com.globalresources.po.User;

/**
 * @author aaron.lu(luzhiyuan) E-mail:atyuan.lu@gmail.com
 * @since 创建时间：Aug 7, 2009 5:13:58 PM
 */
public class MailMessageAciton extends AbstractAction implements ServletRequestAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private IMessageService messageService;
	private IUserService userService;
	private PagerService pagerService;
	
	private String message;
	
	private Message new_message;
	private List<String> types;
	
	private Pager message_inbox_pager;
	private List<MessageReceiver> message_inbox_list;
	private String message_inbox_pagerMethod;
	private String message_inbox_totalRow;
	private String message_inbox_currentPage;
	
	private Pager message_sent_pager;
	private List<Message> message_sent_list;
	private String message_sent_pagerMethod;
	private String message_sent_totalRow;
	private String message_sent_currentPage;
	
	private String receviers;
	//reply
	
	private String message_id;
	private String message_receiver_id;
	private Message exist_message;
	
	private String view_flag;
	
	//search
	private String keyWord;
	
	
	
	private HttpServletRequest request;	
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	/**
	 * <P>创建新消息
	*@author aaron.lu(luzhiyuan)
	*@return
	 */
	public String new_message(){
		new_message = new Message();
		this.receviers = "";
		types = new ArrayList<String>();
		types.add(Constant.SYSTEM_MESSAGE);
		types.add(Constant.PUBLIC_MESSAGE);
		types.add(Constant.PERSONAL_MESSAGE);
		return SUCCESS;
		
	}
	
	/**
	 * <P>回复消息
	*@author liming.yu(yulm88@gmail.com)
	*@return
	 */
	public String reply_message(){
		new_message = new Message();
		if(this.view_flag.equals(Constant.VIEW_FLAG_PHYSITIANLIST) ||
				this.view_flag.equals(Constant.VIEW_FLAG_PHYSITIANRANK))
		{
			this.exist_message = null;
		}
		if(this.exist_message != null)
		{
			this.receviers = this.exist_message.getUser().getUsername();
			this.new_message.setTitle("Re:" + this.exist_message.getTitle());
			this.new_message.setContent(
					
					"\n\n>From:" +this.exist_message.getUser().getUsername()+"\n"+
					">Subject:" +this.exist_message.getTitle()+"\n"+
					">Date:" +this.exist_message.getSendTime()+"\n"+
					">Content:"
					+ this.exist_message.getContent()
					 );
			
		}
		types = new ArrayList<String>();
		types.add(Constant.SYSTEM_MESSAGE);
		types.add(Constant.PUBLIC_MESSAGE);
		types.add(Constant.PERSONAL_MESSAGE);
		return SUCCESS;
		
	}
	
	/**
	 * <P>保存消息
	*@author aaron.lu(luzhiyuan)
	*@return
	 */
	public String save_message(){
		List<User> owners = userService.getUsers(getCurrentUerName());
		if(null != owners && owners.size() > 0){
			new_message.setUser(owners.get(0));
			new_message.setSendTime(new Date());
			new_message.setIsDelete(Constant.BOOLEAN_FALSE);
			String[] users = StringHelper.split(receviers, ",");
			List<User> user_list = userService.getUsers(users);
			if (null != user_list && user_list.size() > 0) {
				for (int i = 0; i < user_list.size(); i++) {
					User user = user_list.get(i);
					new_message.getMessageReceivers().add(initMessageReceiver(user));
				}
			}
		}	
		messageService.saveMessage(new_message);
		receviers = "";
		return SUCCESS;
	}
	
	/**
	 * <p>收件箱
	*@author aaron.lu(luzhiyuan)
	*@return
	 */
	public String message_inbox(){
		String current_username = getCurrentUerName();
		int _total_inbox = messageService.countMessageReceiver(current_username, null, null, Constant.BOOLEAN_FALSE, null);
		message_inbox_pager = pagerService.getPager(message_inbox_currentPage, message_inbox_pagerMethod, _total_inbox);
		message_inbox_list  = messageService.getMessageReceiver(current_username, null, null, Constant.BOOLEAN_FALSE,  null, message_inbox_pager.getPageSize(), message_inbox_pager.getStartRow());
		setMessage_inbox_currentPage(String.valueOf(message_inbox_pager.getCurrentPage()));
		setMessage_inbox_totalRow(String.valueOf(_total_inbox));
		return SUCCESS;
	}
	
	/**
	 * <p>发件箱
	*@author aaron.lu(luzhiyuan)
	*@return
	 */
	public String message_sent(){
		String current_username = getCurrentUerName();
		int _total_sent = messageService.countMessages(current_username, Constant.BOOLEAN_FALSE);
		message_sent_pager = pagerService.getPager(message_sent_currentPage, message_sent_pagerMethod, _total_sent);
		message_sent_list = messageService.getMessages(current_username, Constant.BOOLEAN_FALSE, message_sent_pager.getPageSize(), message_sent_pager.getStartRow());
		setMessage_sent_currentPage(String.valueOf(message_sent_pager.getCurrentPage()));
		setMessage_sent_totalRow(String.valueOf(_total_sent));
		return SUCCESS;
	}
	
	/**
	 * <p>Search Emails 在收件箱中查找
	*@author liming
	*@return
	 */public String searchEmail_inbox(){
		 	//String searchwords=request.getParameter("searchwords");
			String current_username = getCurrentUerName();
			int _total_inbox = 0;
			_total_inbox = messageService.countMatchingMessageReceiver(this.keyWord,current_username, null, null, Constant.BOOLEAN_FALSE, null);
			message_inbox_pager = pagerService.getPager(null, null, _total_inbox);
			if(_total_inbox > 0)
			{
				message_inbox_pager.setPageSize(_total_inbox); //匹配后仅显示为一页内容
			}
			
			message_inbox_list  = messageService.getMatchingMessageReceiver(this.keyWord,current_username,
					null, null, Constant.BOOLEAN_FALSE,  null, message_inbox_pager.getPageSize(), 
					message_inbox_pager.getStartRow());
			setMessage_inbox_currentPage(String.valueOf(message_inbox_pager.getCurrentPage()));
			setMessage_inbox_totalRow(String.valueOf(_total_inbox));
			return SUCCESS; 
	 }
	
	 /**
		 * <p>Search Emails 在发件箱中查找
		*@author liming.yu(yulm88@gmail.com
		*@return
		 */public String searchEmail_sent(){
			 //String searchwords=request.getParameter("searchwords");
			 	String current_username = getCurrentUerName();
			 	int _total_sent = 0;
				_total_sent = messageService.countMatchingMessages(this.keyWord,current_username, Constant.BOOLEAN_FALSE);
				message_sent_pager = pagerService.getPager(message_sent_currentPage, message_sent_pagerMethod, _total_sent);
				if(_total_sent > 0)
				{
					message_inbox_pager.setPageSize(_total_sent); //匹配后仅显示为一页内容
				}
				message_sent_list = messageService.getMatchingMessages(this.keyWord,current_username, Constant.BOOLEAN_FALSE, message_sent_pager.getPageSize(), message_sent_pager.getStartRow());
				setMessage_sent_currentPage(String.valueOf(message_sent_pager.getCurrentPage()));
				setMessage_sent_totalRow(String.valueOf(_total_sent));
				return SUCCESS; 
		 }
	 
	/**
	 * <p>delete Message
	*@author songduanlei@gmail.com
	*@return
	 */	
	public String deleteMessage(){
		String id=request.getParameter("id");
		Message temp_message=messageService.getMessage(Integer.parseInt(id));
		messageService.deleteMessage(temp_message);
		message="delete Success! Please continue your operate";
		return SUCCESS;
	}
	/**
	 * <p>delete Message List 
	*@author songduanlei@gmail.com
	*@return
	 */	
	public String deleteMessageList(){
		String id=request.getParameter("hid");
		System.out.println(id);
		String[] idlist=id.split(",");
		if(idlist.length==1){
			message=" Please select message!";
		
		}else{
		Integer hid;
		for(int i=1; i<idlist.length;i++){
			hid=Integer.parseInt(idlist[i]);
			messageService.deleteMessageById(hid);
			}	
		message="delete Success! Please continue your operate";
		}
		
		return SUCCESS;
	}

	
	/**
	 * <P>s查看邮件详细信息
	*@author aaron.lu(luzhiyuan)
	*@return
	 */
	public String message_detail(){
		exist_message = messageService.getMessage(new Integer(message_id));
		if (view_flag.equals(Constant.VIEW_FLAG_INBOX)) {
			MessageReceiver _MessageReceiver = messageService.getMessageReceiver(new Integer(message_receiver_id));
			_MessageReceiver.setIsReceive(Constant.BOOLEAN_TRUE);
			_MessageReceiver.setIsRead(Constant.BOOLEAN_TRUE);
			messageService.saveMessageReceiver(_MessageReceiver);
		}
		return SUCCESS;
	}
	
	public String message_deleted(){
		
		return SUCCESS;
	}
	
	/**
	 * <p>初始化消息接受者的信息
	*@author aaron.lu(luzhiyuan)
	*@param user
	*@return
	 */
	private MessageReceiver initMessageReceiver(User user){
		MessageReceiver messageReceiver = new MessageReceiver();
		messageReceiver.setUser(user);
		messageReceiver.setMessage(new_message);
		messageReceiver.setIsReceive(Constant.BOOLEAN_FALSE);
		messageReceiver.setIsRead(Constant.BOOLEAN_FALSE);
		messageReceiver.setIsDelete(Constant.BOOLEAN_FALSE);
		messageReceiver.setSendTime(new Date());
		return messageReceiver;
	}
	
	/**
	 * <p>获取当前用户名字
	*@author aaron.lu(luzhiyuan)
	*@return
	 */
	public String getCurrentUerName(){
		String name = "";
		Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if (null != obj && obj instanceof UserDetails){
			UserDetails userDetails = (UserDetails)obj;
			name = userDetails.getUsername();
		}
		return name;
	}
	
	public IMessageService getMessageService() {
		return messageService;
	}
	public void setMessageService(IMessageService messageService) {
		this.messageService = messageService;
	}
	public IUserService getUserService() {
		return userService;
	}
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	public PagerService getPagerService() {
		return pagerService;
	}
	public void setPagerService(PagerService pagerService) {
		this.pagerService = pagerService;
	}

	public Message getNew_message() {
		return new_message;
	}

	public void setNew_message(Message new_message) {
		this.new_message = new_message;
	}

	public List<String> getTypes() {
		return types;
	}

	public void setTypes(List<String> types) {
		this.types = types;
	}

	public String getReceviers() {
		return receviers;
	}

	public void setReceviers(String receviers) {
		this.receviers = receviers;
	}

	

	public Pager getMessage_inbox_pager() {
		return message_inbox_pager;
	}

	public void setMessage_inbox_pager(Pager message_inbox_pager) {
		this.message_inbox_pager = message_inbox_pager;
	}

	public List<MessageReceiver> getMessage_inbox_list() {
		return message_inbox_list;
	}

	public void setMessage_inbox_list(List<MessageReceiver> message_inbox_list) {
		this.message_inbox_list = message_inbox_list;
	}

	public String getMessage_inbox_pagerMethod() {
		return message_inbox_pagerMethod;
	}

	public void setMessage_inbox_pagerMethod(String message_inbox_pagerMethod) {
		this.message_inbox_pagerMethod = message_inbox_pagerMethod;
	}

	public String getMessage_inbox_totalRow() {
		return message_inbox_totalRow;
	}

	public void setMessage_inbox_totalRow(String message_inbox_totalRow) {
		this.message_inbox_totalRow = message_inbox_totalRow;
	}

	public String getMessage_inbox_currentPage() {
		return message_inbox_currentPage;
	}

	public void setMessage_inbox_currentPage(String message_inbox_currentPage) {
		this.message_inbox_currentPage = message_inbox_currentPage;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public Pager getMessage_sent_pager() {
		return message_sent_pager;
	}

	public void setMessage_sent_pager(Pager message_sent_pager) {
		this.message_sent_pager = message_sent_pager;
	}

	public List<Message> getMessage_sent_list() {
		return message_sent_list;
	}

	public void setMessage_sent_list(List<Message> message_sent_list) {
		this.message_sent_list = message_sent_list;
	}

	public String getMessage_sent_pagerMethod() {
		return message_sent_pagerMethod;
	}

	public void setMessage_sent_pagerMethod(String message_sent_pagerMethod) {
		this.message_sent_pagerMethod = message_sent_pagerMethod;
	}

	public String getMessage_sent_totalRow() {
		return message_sent_totalRow;
	}

	public void setMessage_sent_totalRow(String message_sent_totalRow) {
		this.message_sent_totalRow = message_sent_totalRow;
	}

	public String getMessage_sent_currentPage() {
		return message_sent_currentPage;
	}

	public void setMessage_sent_currentPage(String message_sent_currentPage) {
		this.message_sent_currentPage = message_sent_currentPage;
	}

	public Message getExist_message() {
		return exist_message;
	}

	public void setExist_message(Message exist_message) {
		this.exist_message = exist_message;
	}

	public String getView_flag() {
		return view_flag;
	}

	public void setView_flag(String view_flag) {
		this.view_flag = view_flag;
	}

	public String getMessage_id() {
		return message_id;
	}

	public void setMessage_id(String message_id) {
		this.message_id = message_id;
	}

	public String getMessage_receiver_id() {
		return message_receiver_id;
	}

	public void setMessage_receiver_id(String message_receiver_id) {
		this.message_receiver_id = message_receiver_id;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public String getKeyWord() {
		return keyWord;
	}
	public void setKeyWord(String keyWord) {
		this.keyWord = keyWord;
	}

	
	
	
	
		
}
